package com.qmyx.supply.admin.controller;

import com.ytjj.qmyx.supplychain.common.api.CommonPage;
import com.ytjj.qmyx.supplychain.common.api.CommonResult;
import com.ytjj.qmyx.supplychain.common.api.ResultCode;
import com.ytjj.qmyx.supplychain.common.enums.ProductAuditStatusEnums;
import com.ytjj.qmyx.supplychain.common.enums.ProductStatusEnums;
import com.ytjj.qmyx.supplychain.common.exception.ApiException;
import com.ytjj.qmyx.supplychain.common.model.request.*;
import com.ytjj.qmyx.supplychain.mapper.BankerApplyMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.*;
import com.ytjj.qmyx.supplychain.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @Descriptio 测评
 * @ClassName EvaluationController
 * 
 * 
 * @Version 1.0
 **/

@RestController
@RequestMapping("/evaluation")
public class EvaluationController {
    @Autowired
    private BankerSiteService bankerSiteService;
    @Autowired
    private BankerProductService bankerProductService;
    @Autowired
    private BankerProductSpecService bankerProductSpecService;
    @Autowired
    private BankerProductImgService bankerProductImgService;
    @Autowired
    private ProductAttributeValueService productAttributeValueService;
    @Autowired
    private ProductCategoryService productCategoryService;
    @Resource
    private BankerApplyMapper bankerApplyMapper;

    /**
     * 供应商新增商品时判断品牌信息是否存在且上架
     * @return
     */
    @PostMapping("/checkSiteInfo")
    public CommonResult checkSiteInfo() {
        bankerSiteService.checkSiteInfo();
        return CommonResult.success();
    }

    /**
     * @Description 新增供应商商品
     *
     * 
     * 
     * @Version 1.0
     **/
    @PostMapping("/addProduct")
    public CommonResult addProduct(@RequestBody BankerProductRequest bankerProductRequest) {
        return CommonResult.success(bankerProductService.addBankerProduct(bankerProductRequest));
    }

    /**
     * 删除供应商商品
     */
    @GetMapping("/removeBankerProduct")
    public CommonResult removeBankerProduct(@RequestParam("id") Integer id) {
        bankerProductService.removeBankerProduct(id);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    /**
     * @Description 获取供应商商品列表
     *
     * 
     * 
     * @Version 1.0
     **/
    @PostMapping("/getBankerProductList")
    public CommonResult getBankerProduct(@RequestBody BankerProductQueryRequest bankerProductQueryRequest) {
        return CommonResult.success(bankerProductService.getBankerProductList(bankerProductQueryRequest));
    }

    /**
     * 导出供应商产品到excel
     * @param bankerProductQueryRequest
     * @return
     */
    @PostMapping("/exportDataToExcel")
    public CommonResult exportDataToExcel(@RequestBody BankerProductQueryRequest bankerProductQueryRequest){
        return CommonResult.success(bankerProductService.exportDataToExcel(bankerProductQueryRequest));
    }


    /**
     * @Description 更新规格库存
     *
     * 
     * 
     * @Version 1.0
     **/
    @GetMapping("/updateBankerProductStock")
    public CommonResult updateProductLibraryStock(@RequestParam("id") Integer id,
                                                  @RequestParam("stock") Integer stock) {
        bankerProductSpecService.updateBankerProductSpecStock(id, stock);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    @GetMapping("/updateBankerProductPrice")
    public CommonResult updateProductSpecPrice(@RequestParam("id") Integer id,
                                               @RequestParam("price") BigDecimal price,
                                               @RequestParam("salePrice") BigDecimal salePrice) {
        bankerProductSpecService.updateProductSpecPrice(id, price, salePrice);
        return CommonResult.success(ResultCode.SUCCESS);
    }


    /**
     * 查询商品规格表列表
     * @return
     */
    @PostMapping("/getBankerProductSpec")
    public CommonResult getProductSpec(@RequestBody BankerProductSpecRequest bankerProductSpecRequest){
        CommonPage<YxBankerProductSpec> pageList = bankerProductSpecService.getBankerProductSpec(bankerProductSpecRequest);
        return CommonResult.success(pageList);
    }


    /**
     * 新增商品图片
     *
     * @param yxBankerProductImg
     * @return
     */
    @PostMapping("/addBankerProductImg")
    public CommonResult addBankerProductImg(@RequestBody YxBankerProductImg yxBankerProductImg) {
        bankerProductImgService.addBankerProductImg(yxBankerProductImg);
        return CommonResult.success(ResultCode.SUCCESS);
    }


    /**
     * @Description 批量增加商品
     *
     * 
     * 
     */
    @PostMapping("/batchAddImg")
    public CommonResult imgUpload(@RequestBody ImgUploadRequest imgUploadRequest) {
        int count = bankerProductImgService.batchAddImg(imgUploadRequest);
        if(count > 0) {
            return CommonResult.success(ResultCode.SUCCESS);
        }
        return CommonResult.success(ResultCode.FAILED);
    }

    /**
     * @Description 获取商品图片列表
     *
     * 
     * 
     * @Version 1.0
     **/
    @PostMapping("/getBankerProductImgList")
    public CommonResult getBankerProductImgList(@RequestBody BankerProductImgRequest bankerProductImgRequest) {
        return CommonResult.success(bankerProductImgService.getBankerProductImgList(bankerProductImgRequest));
    }

    /**
     * @Description 编辑商品图片
     *
     * 
     * 
     * @Version 1.0
     **/
    @PostMapping("/editBankerProductImg")
    public CommonResult editBankerProductImg(@RequestBody ImgUploadRequest imgUploadRequest) {
        bankerProductImgService.editBankerProductImg(imgUploadRequest);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    /**
     * @Description 删除商品图片
     *f
     * 
     * 
     * @Version 1.0
     **/
    @GetMapping("/deleteBankerProductImg")
    public CommonResult deleteBankerProductImg(@RequestParam("id") Integer id) {
        bankerProductImgService.deleteBankerProductImg(id);
        return CommonResult.success(ResultCode.SUCCESS);
    }


    /**
     * 根据商品id查询选择的sku属性key
     * @param id
     * @return
     */
    @GetMapping(value = "/getSkuKeysbyId")
    public CommonResult getSkuKeysbyId(@RequestParam Integer id) {
        Map<String,Object> datas = productAttributeValueService.getSkuKeysbyId(id);
        return CommonResult.success(datas);
    }

    /**
     * @Description 更新供应商商品信息
     *
     * 
     * 
     * @Version 1.0
     **/
    @PostMapping("/updateBankerProductInfo")
    public CommonResult updateBankerProductInfo(@Valid @RequestBody BankerProductRequest bankerProductRequest) {
        bankerProductService.updateBankerProductInfo(bankerProductRequest);
        return CommonResult.success(ResultCode.SUCCESS);
    }


    @GetMapping("/updateSpecImg")
    public CommonResult updateSpecImg(@Param("id") Integer id,
                                      @Param("imgUrl") String imgUrl) {
        bankerProductSpecService.updateSpecImg(id, imgUrl);
        return CommonResult.success(ResultCode.SUCCESS);
    }


    /**
     * @Description 更新供应商商品信息
     *
     * 
     * 
     * @Version 1.0
     **/
    @PostMapping("/updateBankerProduct")
    public CommonResult updateBankerProduct(@RequestBody BankerProductRequest bankerProductRequest) {
        bankerProductService.updateBankerProduct(bankerProductRequest);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    /**
     * 更新供应商产品库商品信息，免审核
     */
    @PostMapping("/updateProductNoCheck")
    public CommonResult updateProductNoCheck(@RequestBody BankerProductRequest bankerProductRequest) {
        Integer integer = bankerProductService.updateProductNoCheck(bankerProductRequest);
        return integer > 0 ? CommonResult.success() : CommonResult.failed();
    }

    @GetMapping("/updateProductStatus")
    public CommonResult updateProductStatus(@RequestParam("id") Integer id) {
        return CommonResult.success(bankerProductService.updateProductStatus(id));
    }

    @GetMapping("/checkAuditStatus")
    public CommonResult checkAuditStatus(@RequestParam Integer id) {
        Example example = new Example(YxBankerApply.class);
        example.createCriteria().andEqualTo("bankerProductId", id).andEqualTo("type", 2).andEqualTo("status", 2);
        example.orderBy("createTime").desc();
        YxBankerApply bankerApply = bankerApplyMapper.selectOneByExample(example);

        YxBankerProduct bankerProduct = bankerProductService.selectByPrimaryKey(id);
        if (null == bankerApply && 1 == bankerProduct.getAuditStatus()) {
            throw new ApiException("商品编辑需要申请信息审核通过后方可修改");
        }

        if (null == bankerProduct) {
            throw new ApiException("找不到对应的商品信息");
        }
//        if (ProductStatusEnums.UP.getStatus().equals(bankerProduct.getStatus().intValue())
//                || ProductAuditStatusEnums.PASS.getStatus().equals(bankerProduct.getAuditStatus())) {
//            throw new ApiException("商品审核通过不支持修改，需要编辑商品信息，请申请");
//        }
        return CommonResult.success();
    }


    /**
     * 获取三级分类id
     */
    @GetMapping("/getBottomCategoryList")
    public CommonResult getBottomCategoryList(@RequestParam("id") Integer id) {
       return CommonResult.success(productCategoryService.getBottomCategoryList(id));
    }

    @PostMapping("/updateMoreSalePrice")
    public CommonResult updateMoreSalePrice(@RequestBody BankerProductRequest bankerProductRequest) {
        Integer count = bankerProductSpecService.updateMoreSalePrice(bankerProductRequest.getSpecIds(), bankerProductRequest.getSalePrice());
        return CommonResult.success(count);
    }

}
